package com.jmcgg.freesky.common.util;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.jmcgg.freesky.common.dao.FreeskyPOJO;

/**
 * Title: FreeSky Common Java Library <br>
 * Description: FreeSky Common Java Library <br>
 * Copyright: Copyright (c) 2005~2009 www.afss.cn <br>
 * Company: Amei's FreeSky Studio <br>
 * 
 * @author <b>Jacky Chou</b> (AFSS [Amei's FreeSky Studio])
 * @version 1.2.1, 2008-08-05, Clean and optimize the source code.<br>
 *          1.2.0, 2008-02-23, Upgrade freesky.jar to 1.2.0 with Struts 2<br>
 *          1.0.0, 2006-05-30, Initial Version
 */
public class Pager {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(Pager.class);

	private int total; // total number of returned records
	private int beginIndex; // start index number of returned records
	private int endIndex; // end index number of returned records
	private int beginPage; // start page number
	private int endPage; // end page number
	private int count; // record number of returned records
	private int pageSize = 10; // max record number of each page, default value
								// is 10
	private int indexPages = 5; // max index pages in index bar
	private int totalPages; // total pages
	private int curPage; // current page number
	private String query; // save the query condition
	private String pagePath; // page path

	public String getPagePath() {
		return pagePath;
	}

	public void setPagePath(String pagePath) {
		this.pagePath = pagePath;
	}

	public Pager() {
		this.reset();
	}

	public void reset() {
		// quantity
		total = 0;
		count = 0;
		pageSize = 10;
		indexPages = 5;
		totalPages = 0;
		// index
		beginIndex = 0;
		endIndex = 0;
		beginPage = 0;
		endPage = 0;
		curPage = 0;
		query = null;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		if (curPage > this.totalPages - 1) {
			this.curPage = this.totalPages - 1;
		} else {
			this.curPage = curPage;
		}
		if (curPage < 0) {
			this.curPage = 0;
		}

		this.beginIndex = this.curPage * this.pageSize;
		this.endIndex = Math.min(this.total, this.beginIndex + this.pageSize);
		this.beginPage = Math.max(this.curPage - (this.indexPages + 1) / 2 + 1,
				0);
		this.endPage = Math.min(this.beginPage + this.indexPages - 1,
				this.totalPages - 1);
		this.count = this.endIndex - this.beginIndex;
	}

	public int getTotal() {
		return total;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getIndexPages() {
		return indexPages;
	}

	public int getEndPage() {
		return endPage;
	}

	public int getEndIndex() {
		return endIndex;
	}

	public int getCount() {
		return count;
	}

	public int getBeginPage() {
		return beginPage;
	}

	public int getBeginIndex() {
		return beginIndex;
	}

	/**
	 * This method should be invoked before setTotal and setCurPage
	 * 
	 * @param indexPages
	 *            int
	 */
	public void setIndexPages(int indexPages) {
		this.indexPages = indexPages;
	}

	/**
	 * This method should be invoked before setTotal and setCurPage
	 * 
	 * @param pageSize
	 *            int
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * This method had better be invoked before setCurPage
	 * 
	 * @param total
	 *            int
	 */
	public void setTotal(int total) {
		this.total = total;
		this.totalPages = (this.total - 1) / this.pageSize + 1;
	}

	public static void setRequest(HttpServletRequest request, Pager pager) {
		request.removeAttribute(Pager.class.getName());
		request.setAttribute(Pager.class.getName(), pager);
	}

	public void setRequest(HttpServletRequest request) {
		request.removeAttribute(Pager.class.getName());
		request.setAttribute(Pager.class.getName(), this);
	}

	/**
	 * 灏嗚杩斿洖鐨勫垪琛ㄩ噸鏂拌繘琛屾帓鍒楋紝缁勭粐鎴愯〃鏍煎舰寮忓啀杩斿洖
	 * 
	 * @param list
	 *            ArrayList<FreeskyPOJO> 鍘熺粨鏋滈泦锛岀嚎鎬у垪琛�
	 * @param cols
	 *            int 鐩爣琛ㄦ牸鐨勫垪鏁�
	 * @param rows
	 *            int 鐩爣琛ㄦ牸鐨勮鏁�
	 * @return ArrayList 閲嶆柊缁勭粐鍚庣殑瀵硅薄
	 */
	public static ArrayList<ArrayList<FreeskyPOJO>> reform(
			ArrayList<FreeskyPOJO> list, int cols, int rows) {
		ArrayList<ArrayList<FreeskyPOJO>> ret = new ArrayList<ArrayList<FreeskyPOJO>>();
		if (list == null || list.size() == 0 || cols == 0 || rows == 0) {
			return ret;
		}
		ArrayList<FreeskyPOJO> tmp = null;
		int tm = 0;
		int tn = 0;
		try {
			for (int i = 0; i < list.size(); i++) {
				if (tm == 0) {
					tmp = new ArrayList<FreeskyPOJO>();
				}
				tmp.add(list.get(i));
				if (++tm == cols) {
					ret.add(tmp);
					tmp = null;
					tm = 0;
					if (++tn == rows) {
						break;
					}
				}
			}
			if (ret.size() < rows && tmp != null) {
				ret.add(tmp);
			}
		} catch (Exception e) {
			logger.error("reform(ArrayList<FreeskyPOJO>, int, int)", e);
		}
		return ret;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}
}
